package thread.volatile_base;

/**
 * Created by mHandler on 2018/5/30 0030.
 * Description:volatile关键字实现变量可见性，在子线程（私有堆栈）和主线程（公共堆栈）间保证变量可见性
 */
public class RunThread extends Thread {

    volatile private boolean isRunning =true;
    @Override
    public void run() {
        super.run();
        System.out.println("running==========");
        while (isRunning){

        }
        System.out.println("stop running==========");
    }


    public void setRunning(boolean running) {
        isRunning = running;
    }

    public static void main(String[] args) {
        try {
            RunThread thread = new RunThread();
            thread.start();
            Thread.sleep(2000);
            thread.setRunning(false);
            System.out.println("isRunning-->false");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
